//
//  GLGameViewController.m
//  QAQ打飞机
//
//  Created by admin on 15/8/28.
//  Copyright (c) 2015年 gl. All rights reserved.
//

#import "GLGameViewController.h"
//数据
#import "GLImageResource.h"
#import "GLSoundToo.h"
#import "GLGameModel.h"
#import "GLEquipVV.h"
//视图
#import "GLBackGroundView.h"
#import "GLHeroView.h"
#import "GLBulletView.h"
#import "GLEquipView.h"
#import "GLEnemyView.h"
#import "GLBombVIiew.h"

//#import "GLEnterView.h"

#import "GLBullet.h"
#import "GLEquip.h"
#import "GLChartsCell.h"
#pragma mark -步长
static NSInteger step;

@interface GLGameViewController ()
{
    //图像资源
    GLImageResource *_imagesRes;
    //声音资源
    GLSoundToo *_soundToo;
    //游戏模型
    GLGameModel *_gm;
    //背景视图
    GLBackGroundView *_backView;
    //英雄视图
    GLHeroView*_heroView;
    //敌机视图
    GLEnemyView*_enemyVIEW;
    //游戏视图
    UIView *_gameView;
    
    //排行榜视图
    UIView *_scoresView;
    //得分标签
    UILabel *_scoreLabel;
    //记录炸弹数标签
    UILabel*_bombLabel;
    GLBombVIiew*_bombView;
    GLEquipView*_equipView;
    GLEquip*_equip;
    //游戏时钟
    CADisplayLink *_gameTimer;
    //记录屏幕上现有子弹视图的集合
    NSMutableSet *_curScreenBulletViews;
    //记录屏幕上现有敌机视图的集合
    NSMutableSet *_curScreenEnemyViews;
    //记录屏幕上现有掉落物品的集合
    NSMutableSet *_curScreenBombViews;
    GLEnemy *_enemy;
    //按钮
    //炸弹按钮
    UIButton*_bombButton;
    //翅膀按钮
    UIButton *_wingButton;
    //排行榜按钮
    UIButton *_score;
    UIAlertView *_sheet;
    GLChartsCell *_chartView;
}
@end

@implementation GLGameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    step =0;
    //初始化集合
    _curScreenBulletViews =[NSMutableSet set];
    _curScreenEnemyViews =[NSMutableSet set];
    _curScreenBombViews =[NSMutableSet set];
    NSString *st=NSHomeDirectory();
    NSLog(@"%@",st);
    //加载
    //播放背景音乐
    [_soundToo playBGMusic];
    //实例化游戏模型
    _gm =[GLGameModel gameModelWithArea:self.view.bounds size:[_imagesRes.heroFlyImages[0] size]];
    _gm.hero.normalBulletSize = _imagesRes.normalBulletImage.size;
    _gm.hero.enhancedBulletSize = _imagesRes.enhancedBulletImage.size;
    
    //初始化游戏视图
    _gameView =[[UIView alloc]initWithFrame:_gm.gameArea];
    [self.view addSubview:_gameView];
    //实例化背景视图
    _backView =[[GLBackGroundView alloc]initWithFrame:_gm.gameArea image:_imagesRes.backImage];
    [_gameView addSubview:_backView];
    
    //实例化英雄视图
    _heroView =[[GLHeroView alloc]initWithImages:_imagesRes.heroFlyImages];
    [_heroView setCenter:_gm.hero.position];
    [_gameView addSubview:_heroView];
    //加载排行榜视图
   
    //加载按钮和得分
    [self loadButtonAndScoreLabel];
    //加载炸弹按钮
    [self loadButtonBomb];
    //加载翅膀按钮
    [self loadButtonWing];
    //初始化游戏时钟
    [self startGameTimer];
    //排行榜
    [self loadBeginScoreButton];
}

#pragma mark -私有方法
#pragma mark -加载按钮和得分
-(void)loadButtonAndScoreLabel
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:_imagesRes.btnPauseImage forState:UIControlStateNormal];
    //[btn setImage:_imagesRes.btnpauseImageHL forState:UIControlStateHighlighted];
    CGSize size =_imagesRes.btnPauseImage.size;
    [btn setFrame:CGRectMake(kPauseStartBtnXY, kPauseStartBtnXY, size.width/3.0, size.height/2.0)];
    [btn addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    //得分标签
    CGFloat x =btn.frame.origin.x+btn.frame.size.width;
    CGFloat y =btn.frame.origin.y;
    CGFloat w =_gm.gameArea.size.width-x-kPauseStartBtnXY;
    CGFloat h =btn.frame.size.height;
    
    _scoreLabel =[[UILabel alloc]initWithFrame:CGRectMake(x, y, w, h)];
    [_scoreLabel setFont:[UIFont fontWithName:@"Maeker Felt" size:23.0f]];
    [_scoreLabel setTextColor:[UIColor darkGrayColor]];
    [self.view addSubview:_scoreLabel];

}
#pragma mark 暂停按钮的监听方法
-(void)tapButton:(UIButton *)sender
{
    sender.tag =!sender.tag;
    UIImage*image =nil;
    UIImage *imageHL =nil;
    if (sender.tag == 1)
    {
        //暂停游戏，显示的是开始的图像
        image =_imagesRes.btnStartImage;
        imageHL =_imagesRes.btnStartImageHL;
        //停止游戏时钟
        [self stopGameTimer];
        
    }
    else
    {
        //开始游戏
        image =_imagesRes.btnPauseImage;
        imageHL =_imagesRes.btnpauseImageHL;
        //开启游戏时钟
        [self startGameTimer];
    }
    [sender setImage:image forState:UIControlStateNormal];
    //[sender setImage:imageHL forState:UIControlStateHighlighted];

}
#pragma mark 更新得分标签
-(void)updateScoreLabel
{
  //有分数才显示
    NSString *strContent = @"";
    if (_gm.gameScore >0) {
        strContent = [NSString stringWithFormat:@"%ld000",_gm.gameScore];
    }
    [_scoreLabel setText:strContent];
   
}
#pragma mark 放炸弹按钮
-(void)loadButtonBomb
{
    _bombButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_bombButton setImage:_imagesRes.btnBomb forState:UIControlStateNormal];
   // [_bombButton setImage:_imagesRes.btnBombHL forState:UIControlStateHighlighted];
    CGSize size =_imagesRes.btnBombButton.size;
    [_bombButton setFrame:CGRectMake(kPauseStartBtnXY, self.view.bounds.size.height-size.height,size.width,size.height)];
    //[_bombButton addTarget:self action:@selector(bombButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_bombButton];
    //加载炸弹文本
//    CGFloat labelX = _bombButton.frame.origin.x + _bombButton.frame.size.width;
//    CGFloat labelY = _bombButton.frame.origin.y;
//    CGFloat labelW = _bombButton.frame.size.width*2;
//    CGFloat labelH = _bombButton.frame.size.height;
//    _bombLabel = [[UILabel alloc]initWithFrame:CGRectMake(labelX, labelY, labelW, labelH)];
//    [self.view addSubview:_bombLabel];
//    [_bombLabel setText:@"x00"];
//    [_bombLabel setFont:[UIFont fontWithName:@"Marker Felt" size:20.0f]];
//    [_bombLabel setTextColor:[UIColor greenColor]];

}
#pragma mark 炸弹按钮监听
-(void)bombButton:(UIButton*)sender
{
    if (sender.tag==1) {
    [_soundToo playAudioWithName:arc4random_uniform(1)?kBomb1:kBomb2];
    [_bombButton setImage:_imagesRes.btnBomb forState:UIControlStateNormal];
    //[_curScreenBombViews removeAllObjects];
    for (GLEnemyView*ev in _curScreenEnemyViews) {
        ev.enemy.bBlowup=YES;
    }
        sender.tag=0;
    }
}
#pragma mark 翅膀按钮
-(void)loadButtonWing
{
    _wingButton=[UIButton buttonWithType:UIButtonTypeCustom];
    
    CGSize size =[_imagesRes.equipImage[0] size];
    [_wingButton setFrame:CGRectMake(kPauseStartBtnXY*6, self.view.bounds.size.height-size.height/2.0,size.width/2.0,size.height/2.0)];
    [self.view addSubview:_wingButton];

}
-(void)wingButton:(UIButton *)sender
{
    if (sender.tag==1) {
        
    _equipView =[[GLEquipView alloc]initWithImages:_imagesRes.equipImage];
    [_equipView setCenter:_gm.hero.position];
    [_gameView addSubview:_equipView];
        //增强子弹效果
        _gm.hero.bEnhanced = YES;
        sender.tag=0;
    }
    

}
#pragma mark -加载排行榜按钮
-(void)loadBeginScoreButton
{
    _score=[UIButton buttonWithType:UIButtonTypeCustom];
    [_score setImage:_imagesRes.BeginScoreImage forState:UIControlStateNormal];
    CGSize size =_imagesRes.BeginScoreImage.size;
    [_score setFrame:CGRectMake(self.view.frame.size.width-size.width/2.0,0, size.width/2.0, size.height/2.0)];
    [_score addTarget:self action:@selector(ChartsView:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_score];
    
}

- (void)ChartsView:(UIButton *)sender
{
    //GLChartsView *p=[[GLChartsView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    sender.tag =!sender.tag;
    UIImage*image =nil;
  //  UIImage *imageHL =nil;
    if (sender.tag == 1)
    {
        //暂停游戏，显示图像
        image =_imagesRes.Returnimage;
        [sender setImage:_imagesRes.ReturnimageHL forState:UIControlStateHighlighted];
        
        //停止游戏时钟
        [self stopGameTimer];
        
        //加载排行榜
        
        
    }
    else
    {
        image =_imagesRes.BeginScoreImage;

        //开启游戏时钟
        [self startGameTimer];
    }
    [sender setImage:image forState:UIControlStateNormal];

}

#pragma mark 触摸事件
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   //1.取得手指
    UITouch *tounch = [touches anyObject];
    //2.取得手指的当前位置
    CGPoint loc =[tounch locationInView:_gameView];
    //3.取得手指的之前位置
    CGPoint pre = [tounch previousLocationInView:_gameView];
    //4.求差
    CGPoint offset =CGPointMake(loc.x-pre.x, loc.y-pre.y);
    //5.求英雄现在要移动的位置
    CGPoint curPosition =_gm.hero.position;
    _gm.hero.position =CGPointMake(curPosition.x+offset.x, curPosition.y+offset.y);
}
#pragma mark 时钟触发的方法
-(void)step
{
    step++;
   //1.先移动
    [_gm backMoveDown];
   //2.更新
    [_backView updateBGWithFrame:_gm.backFrame1 frame:_gm.backFrame2];
    //更新英雄的位置
    [_heroView setCenter:_gm.hero.position];
    [_equipView setCenter:_gm.hero.position];
    //发射子弹
    //一秒发射三次
    if (step%20 ==0)
    {
        [_gm.hero fire3Bullet];
    }
    //显示子弹，移动
    [self showBulletAndCheckMove];
    //飞机
    if (step%20==0)
    {
        GLEnemytype type =kEnemySmall;
        CGSize size =_imagesRes.smallEnemyFlyImage.size;
        if (step%(9*60)==0)
        {
            //随机出现中大飞机
            size =_imagesRes.midEnemyFlyImage.size;
            type =(arc4random_uniform(3)==0)?kEnemyBig:kEnemyMid;
            if (type==kEnemyBig)
            {
                size =[_imagesRes.bigEnemyFlyImages size];
                //播放大飞机出场音效
                [_soundToo playAudioWithName:kEnemyBigOutAudio];
            }
        }
        GLEnemy*enemy =[_gm createEnemyWithType:type size:size];
        //显示
        GLEnemyView *ev =[[GLEnemyView alloc]initWithEnemy:enemy imagesRes:_imagesRes];
        [_gameView addSubview:ev];
        [_curScreenEnemyViews addObject:ev];
    }
    
    //掉落物品
       if (step%(15*60)==0)
    {
        GLEquipType type =(arc4random_uniform(3)==0)?knBullet:knBomb;
        CGSize size =_imagesRes.btnBomb.size;
        //随机出现掉落物品
        if (type==knBullet) {
            size =[_imagesRes.equipImage[0] size];
            [_wingButton addTarget:self action:@selector(wingButton:) forControlEvents:UIControlEventTouchUpInside];
            _wingButton.tag=1;
        }
        else if(type==knBomb)
        {
        // size =_imagesRes.btnBomb.size;
        [_bombButton addTarget:self action:@selector(bombButton:) forControlEvents:UIControlEventTouchUpInside];
            _bombButton.tag=1;
        }
        GLEquipVV*p=[_gm createEquipWithType:type size:size];
        GLBombVIiew *pv =[[GLBombVIiew alloc]initWithEquip:p imagesRes:_imagesRes];
        [_gameView addSubview:pv];
        [_curScreenBombViews addObject:pv];
    }
   

        //显示敌机并移动
    [self showEnemyAndCheckMove];
    //碰撞检测
    [self collisionDetector];
    //显示
    [self showbomb];
    
}
#pragma mark -碰撞检测
-(void)collisionDetector
{
  //爆炸动画播放太快
    if (step%10==0)
    {
        //遍历要播放爆炸动画的敌机
        NSMutableSet *tempSet =[NSMutableSet set];
        for (GLEnemyView*ev in _curScreenEnemyViews)
        {
            if (ev.enemy.bBlowup)
            {
                //播放爆炸动画
                [ev setImage:ev.blowupImages[ev.enemy.blowupFrames]];
                ev.enemy.blowupFrames++;
                //如果播放完成的话。删除此敌机
                if (ev.enemy.blowupFrames==ev.blowupImages.count)
                {
                    [tempSet addObject:ev];
                }
            }
        }
        //删除爆炸了的敌机
        for (GLEnemyView*ev in tempSet)
        {
            //更新得分
            _gm.gameScore +=ev.enemy.score;
            //更新标签显示
            [self updateScoreLabel];
            //父视图中删除
            [ev removeFromSuperview];
            //从当前屏幕集合中删除
            [_curScreenEnemyViews removeObject:ev];
        }
        [tempSet removeAllObjects];
    }
    //碰撞检测
    //记录已经使用过的子弹
    NSMutableSet *temBulletSet = [NSMutableSet set];
    for (GLBulletView *bv in _curScreenBulletViews)
    {
        GLBullet *b= bv.bullet;
        for (GLEnemyView *ev in _curScreenEnemyViews)
        {
            GLEnemy *e=ev.enemy;
            if (ev.enemy.bBlowup)
            {
                //如果当前敌机已经在播放爆炸动画时，则不再消耗子弹
                continue;
            }
            if (CGRectIntersectsRect(bv.frame, ev.frame))
            {
                //用敌机的生命值-子弹的伤害
                e.hp=e.hp-b.damage;
                if (e.hp<=0) {
                    //爆炸
                    e.bBlowup =YES;
                    if (ev.enemy.type == kEnemySmall)
                    {
                        [_soundToo playAudioWithName:kEnemySmallAudio];
                    }
                    else if (ev.enemy.type ==kEnemyMid)
                    {
                        [_soundToo playAudioWithName:kEnemyMidAudio];
                    }
                    else
                    {
                        [_soundToo playAudioWithName:kEnemyBigAudio];
                    }
                }
                else{
                //显示被挨揍的图像
                //如果打中的是大飞机
                    if (e.type == kEnemyBig)
                    {
                        [ev stopAnimating];
                    }
                    [ev setImage:ev.hitImage];
                }
                //如果子弹击中了敌机，子弹被消耗
                [temBulletSet addObject:bv];
                break;
            }
        }
    }
    //删除已经使用过的子弹
    for (GLBulletView *bv in temBulletSet)
    {
        [bv removeFromSuperview];
        [_curScreenBulletViews removeObject:bv];
    }
    [temBulletSet removeAllObjects];
    //敌机撞英雄
   for (GLEnemyView*ev in _curScreenEnemyViews)
    {
        if (CGRectIntersectsRect(ev.frame, _gm.hero.collisionFrame)&&!ev.enemy.bBlowup)
       {
           //英雄挂了
            [_soundToo playAudioWithName:kHeroOver];
           //先停止序列帧动画
            [_heroView stopAnimating];
           //播放英雄爆炸动画
           NSInteger count =_imagesRes.heroBlowupImages.count;
            [_heroView setImage:_imagesRes.heroBlowupImages[count - 1]];
            [_heroView setAnimationImages:_imagesRes.heroBlowupImages];
            [_heroView setAnimationDuration:1.0f];
            [_heroView setAnimationRepeatCount:1];
            [_heroView startAnimating];
            //应该在英雄爆炸动画播放完成后,才停止游戏
            //[self performSelector:@selector(namek) withObject:nil afterDelay:0];
           [_soundToo playAudioWithName:kVoice5];
           
           //因为英雄一撞就死   所以跳出
           [self stopGameTimer];
        
          // if ([Manangedcharts selectedScoreWith:[_scoreLabel.text integerValue]]&&_scoreLabel.text.integerValue!=0) {
               //弹出输入昵称
               UIAlertView *alerView =[[UIAlertView alloc]initWithTitle:@"登榜啦！可以哟！请留下您的游戏昵称！" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
               [alerView setAlertViewStyle:UIAlertViewStylePlainTextInput];
               [alerView show];
          // }
//           else
//           {
//               
//               UIAlertView*alerView =[[UIAlertView alloc]initWithTitle:@"您需要增强！可选择一下路径！" message:@"充值！！！！！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
//               [alerView show];
//               
//           }

          
          
           break;
        }
        
        
}
    //掉落物品撞英雄
    //记录已经碰撞了的
    NSMutableSet *temp = [NSMutableSet set];
    for (GLBombVIiew *pv in _curScreenBombViews)
    {
        if (CGRectIntersectsRect(pv.frame, _gm.hero.collisionFrame))
        {
         
            if (pv.equip.equiptype==knBomb)
            {
                [_bombButton setImage:_imagesRes.btnBombHL forState:UIControlStateNormal];
               
            }
            else if(pv.equip.equiptype==knBullet)
            {
              [_wingButton setImage:_imagesRes.equipImage[0] forState:UIControlStateNormal];
               
                //加载音效
                [_soundToo playAudioWithName:kArticle];
            }
            [temp addObject:pv];
         
        }
    }
        for (GLBombVIiew*pv in temp) {
            
       
        [pv removeFromSuperview];
        [_curScreenBulletViews removeObject:pv];
        }
    
}
#pragma mark 输入昵称

#pragma maek 排行榜
-(void)rankingScoreWithUserName:(NSString*)userName
{
    NSLog(@"%@--------1",userName);
    NSLog(@"%@--------2",@([_scoreLabel.text integerValue]));
    CGRect rect = CGRectMake(20, 50, _gameView.bounds.size.width - 40, _gameView.bounds.size.height - 100);
    _chartView = [[GLChartsCell alloc]initWithFrame:rect image:_imagesRes];
    //提交分数和用户名到coreData
    [Manangedcharts insertNewScoreWith:@([_scoreLabel.text integerValue]) name:userName];

    
    
    //显示排行榜
    NSArray *dastaList = [Manangedcharts selectedAllScore];
    if (dastaList.count < _chartView.scoreLabel.count)
    {
        for (int i = 0; i<dastaList.count; i++)
        {
            Heoll *s = dastaList[i];
            UILabel *label = _chartView.scoreLabel[i];
            [label setText:[s.score stringValue]];
            NSLog(@"%@--------2",[s.score stringValue]);
            UILabel *label2 = _chartView.nameLabel[i];
            [label2 setText:s.name];
             NSLog(@"%@--------3",s.name);
        }
    }
    else
    {
        [_chartView.scoreLabel enumerateObjectsUsingBlock:^(UILabel *obj, NSUInteger idx, BOOL *stop) {
            Heoll *s = [Manangedcharts selectedAllScore][idx];
            [obj setText:[s.score stringValue]];
        }];
        [_chartView.nameLabel enumerateObjectsUsingBlock:^(UILabel *obj, NSUInteger idx, BOOL *stop) {
            Heoll *s = [Manangedcharts selectedAllScore][idx];
            [obj setText:s.name];
        }];
    }
    
    
    
    [_gameView addSubview:_chartView];
    
    [_gameView addSubview:_chartView.exitBtn];
    [_gameView addSubview:_chartView.playBtn];
    
    //添加按钮事件
    [_chartView.exitBtn addTarget:self action:@selector(tapRankButton:) forControlEvents:UIControlEventTouchUpInside];
    [_chartView.playBtn addTarget:self action:@selector(tapRankButton:) forControlEvents:UIControlEventTouchUpInside];

}
#pragma mark 排行榜按钮点击事件
-(void)tapRankButton:(UIButton *)sender
{
    if (sender.tag == 0)
    {
        [self exit];
    }
    else
    {
        [self exit];
        //[self playAgain];
    }
}
#pragma mark -退出游戏方法
-(void)exit
{
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    UIWindow *window = app.window;
    [UIView animateWithDuration:1.0f animations:^{
        [window setAlpha:0];
        window.frame = CGRectMake(0, window.frame.size.width, 0, 0);
    } completion:^(BOOL finished) {
        exit(0);
    }];
}

#pragma mark -游戏时钟操作
#pragma mark 开启游戏时钟
-(void)startGameTimer
{
    //1)实例化游戏时钟
    _gameTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(step)];
    //2)添加到主运行循环
    [_gameTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}

#pragma mark 停止游戏时钟
-(void)stopGameTimer
{
    //停止游戏
    [_gameTimer invalidate];
}

#pragma mark -显示敌机并向下移动敌机视图
-(void)showEnemyAndCheckMove
{
  //用来记录移出屏幕的敌机
    NSMutableSet *removeSet = [NSMutableSet set];
    for (GLEnemyView *ev in _curScreenEnemyViews)
    {
       // NSLog(@"%@--1",ev);
        CGPoint position =CGPointMake(ev.center.x, ev.center.y+ev.enemy.speed);
        [ev setCenter:position];
        //判断 是否移出屏幕
        if (CGRectGetMinY(ev.frame)>_gm.gameArea.size.height)
        {
            [removeSet addObject:ev];
        }
    }
    //删除已经不在屏幕上的敌机
    for (GLEnemyView *ev in removeSet)
    {
        [ev removeFromSuperview];
        [_curScreenEnemyViews removeObject:ev];
    }
    [removeSet removeAllObjects];
    
}
#pragma mark -显示掉落物品
-(void)showbomb
{
   //用来记录移出屏幕的物品
    NSMutableSet *removeSet = [NSMutableSet set];
    for (GLBombVIiew *pv in _curScreenBombViews)
    {
        CGPoint position =CGPointMake(pv.center.x, pv.center.y+pv.equip.speed);
        [pv setCenter:position];
        //判断 是否移出屏幕
        if (CGRectGetMinY(pv.frame)>_gm.gameArea.size.height)
        {
            [removeSet addObject:pv];
        }
    }
    //删除已经不在屏幕上的物品
    for (GLBombVIiew*pv in removeSet)
    {
        [pv removeFromSuperview];
        [_curScreenBombViews removeObject:pv];
    }
    [removeSet removeAllObjects];

}
#pragma mark -显示子弹并向上移动子弹
-(void)showBulletAndCheckMove
{
   //移动屏幕上已经有子弹 5
    NSMutableSet *temp = [NSMutableSet set];
    for (GLBulletView *bv in _curScreenBulletViews)
    {
        GLBullet*b =bv.bullet;
        CGPoint position = CGPointMake(b.position.x, b.position.y-5);
        b.position = position;
        [bv setCenter:position];
        //如果移出了屏幕，则记录下来
        if (CGRectGetMaxY(bv.frame)<0)
        {
            [temp addObject:bv];
        }
    }
    //消除掉已经打出屏幕的子弹
    for (GLBulletView *bv in temp)
    {
        //从屏幕删除
        [bv removeFromSuperview];
        //从屏幕子弹视图集合删除
        [_curScreenBulletViews removeObject:bv];
    }
    [temp removeAllObjects];
     //显示英雄体内的子弹
    for (GLBullet *b in _gm.hero.heroBulletSet)
    {
        UIImage *image = _imagesRes.normalBulletImage;
        if (_gm.hero.bEnhanced)
        {
            image = _imagesRes.enhancedBulletImage;
        }
        //播放子弹音效
        [_soundToo playAudioWithName:kBulletAudio];
        GLBulletView *bv =[[GLBulletView alloc]initWithImage:image bullet:b];
        [bv setCenter:b.position];
        [_gameView addSubview:bv];
        //添加到集合
        [_curScreenBulletViews addObject:bv];
    }
   //清空已经添加到屏幕上的英雄体内的子弹集合
    [_gm.hero.heroBulletSet removeAllObjects];
}
#pragma mark -加载资源方法
-(void)loadResources
{
    _imagesRes =[[GLImageResource alloc]init];
    _soundToo =[[GLSoundToo alloc]init];
}

#pragma mark -alertView代理方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        //再玩一局
        [self playAgain];
    }
    else
    {
        if (alertView.message == nil)
        {
            //得到用户名显示排行榜
            //得到用户名
            UITextField *text = [alertView textFieldAtIndex:0];
            if (text.text.length == 0)
            {
                text.text = @"无名";
            }
            //显示排行榜
            [self rankingScoreWithUserName:text.text];
        }
        else
        {
            //退出游戏
            [self exit];
        }
    }
}

#pragma mark -再玩一局
-(void)playAgain
{
    //设置暂停、炸弹按钮可用
    //[_pauseBtn setEnabled:YES];
    //[_bombBtn setEnabled:YES];
    
    
    //删除牺牲英雄
    [_heroView removeFromSuperview];
    
//    //实例化英雄图像
//    _heroView =[[GLHeroView alloc]initWithImages:_imagesRes.heroFlyImages];
//    [_gameView addSubview:_heroView];
//    CGFloat x = _gm.gameArea.size.width / 2.0f;
//    CGFloat y = _gm.gameArea.size.height - _gm.hero.size.height;
//    _gm.hero.heroPosition = CGPointMake(x, y);
//    
//    //删除当前屏幕敌机视图
//    for (XFEnemyView *ev in _curEnemySet) {
//        [ev removeFromSuperview];
//    }
//    [_curEnemySet removeAllObjects];
//    
//    //删除当前屏幕上的子弹视图
//    for (XFBulletView *bv in _curBulletSet) {
//        [bv removeFromSuperview];
//    }
//    [_curBulletSet removeAllObjects];
//    
//    //删除当前屏幕上的补给
//    for (XFSupplyView *sv in _curSupplySet)
//    {
//        [sv removeFromSuperview];
//    }
//    [_curSupplySet removeAllObjects];
//    
//    //删除排行榜视图
//    [_rankView.exitBtn removeFromSuperview];
//    [_rankView.playBtn removeFromSuperview];
//    [_rankView removeFromSuperview];
//    
    //初始化子弹为不增强效果
    _gm.hero.bEnhanced = NO;
    //初始化并更新得分文本
    _gm.gameScore = 0;
    [self updateScoreLabel];
    //初始化并更新炸弹文本
    
    //开始游戏
    [self startGameTimer];
}




@end
